﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Kristall.Screens;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Kristall;
using Kristall.Editor.Commands;
using Kristall.Engine;
using Kristall.Managers;


namespace Kristall
{

    public class SelectTool : Tool
    {
        public override string CursorTexture
        {
            get { return "Select"; }
        }

        public override Keys Hotkey
        {
            get { return Keys.D2; }
        }

        public override Vector2 CursorOffset
        {
            get
            {
                return new Vector2(16, 0);
            }
        }

        public string[] SelectionGroup { get; set; }

        public List<AABB> SelectableObjects
        {
            get
            {
                List<AABB> temp = new List<AABB>();

                if (SelectionGroup.Contains("Actors"))
                    temp.AddRange(LevelEditor.Level.NPCs);

                if (SelectionGroup.Contains("Geometry"))
                    temp.AddRange(LevelEditor.Level.Animatables);

                if(SelectionGroup.Contains("Lights"))
                    temp.AddRange(LevelEditor.Level.Lights);

                temp.AddRange(LevelEditor.Level.Crystals);

                //temp.Add(LevelEditor.Level.StartLocation);
                temp.Add(LevelEditor.Level.WinTrigger);

                return temp;
            }
        }

        private bool _isDragging = false;
        Vector2 dragOffset = Vector2.Zero;

        Vector2 dragStartPosition = Vector2.Zero;
        Vector2 dragEndPosition = Vector2.Zero;

        public SelectionManager SelectionManager = new SelectionManager();

        private AABB _selectedObject
        {
            get
            {
                return SelectionManager.SelectedObject;
            }
        }

        public SelectTool(LevelEditor editor)
            : base(editor)
        {
            SelectionGroup = new string[]{};
        }

        public override void Reset()
        {
            base.Reset();
            _isDragging = false;
        }

        public override void MouseUp(Vector2 mousePosition)
        {
            base.MouseUp(mousePosition);
            endDrag();
        }

        public override void MouseDown(Vector2 mousePosition)
        {
            base.MouseDown(mousePosition);

            _isDragging = false;
            Point mousePoint = new Point((int)mousePosition.X, (int)mousePosition.Y);

            AABB previousSelection = null;

            //Check if we're dragging a previous selection
            if (SelectionManager.Contains(mousePoint))
            {
                previousSelection = SelectionManager.SelectedObject;
            }

            //Start a new selection
            SelectionManager.ClearSelection();

            if (SelectableObjects == null)
                return;

            foreach (AABB o in SelectableObjects)
            {
                if (o.Bounds.Contains(mousePoint))
                {
                    SelectionManager.AddSelection(o);
                }
            }

            if (previousSelection != null)
            {
                SelectionManager.Select(previousSelection);
            }

            //If we selected anything, start dragging!
            if (_selectedObject != null)
            {
                startDrag();
            }
        }

        void endDrag()
        {
            if (_selectedObject != null)
            {
                if(!dragStartPosition.Equals(dragEndPosition))
                    LevelEditor.ToolPalette.CommandManager.ExecuteCommand(new MoveCommand(_selectedObject, dragEndPosition));
            }

            _isDragging = false;
        }

        void startDrag()
        {
            dragStartPosition = dragEndPosition = _selectedObject.Position;
            dragOffset = MousePositionWorld - dragStartPosition;
            _isDragging = true;
        }

        public override void HandleInput(InputState input)
        {
            base.HandleInput(input);

            int scroll = CurrentMouseState.ScrollWheelValue - LastMouseState.ScrollWheelValue;
            if (scroll < 0)
            {
                SelectionManager.NextSelection();
            }
            else if (scroll > 0)
            {
                SelectionManager.PreviousSelection();
            }
        }


        public override void Update(float dt)
        {
            base.Update(dt);

            if (_isDragging && _selectedObject != null)
            {
                //int x = (int)(MousePosition.X - dragOffset.X);
                //int y = (int)(MousePosition.Y - dragOffset.Y);
                //SelectedObject.Position = new Vector2(x, y);

                dragEndPosition = MousePositionWorld - dragOffset;
            }

            /*if (Mouse.GetState().MiddleButton == ButtonState.Pressed || _isDragging)
            {
                ScrollCamera();
            }*/
        }

        public override void Draw(SpriteBatch batch)
        {
            if (_selectedObject != null)
            {
                _selectedObject.DrawDebug(batch, Color.Tomato * 0.33f);

                if (_isDragging)
                {
                    Vector2 oldPosition = _selectedObject.Position;
                    _selectedObject.Position = dragEndPosition;
                    if (_selectedObject is Animatable)
                    {
                        ((Animatable)_selectedObject).Draw(batch);
                    }
                    else
                    {
                        _selectedObject.DrawDebug(batch, Color.Azure * 0.33f);
                    }
                    _selectedObject.Position = oldPosition;
                }
            }

            base.Draw(batch);
        }

    }

}
